<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Default controller.
 */
class Category_Controller extends Template_Controller 
{
	public function __construct()
	{
		parent::__construct();

		$auth = new Auth();
		
		if (!$auth->logged_in())
		{
			url::redirect('auth/login', 301);
			exit;
		}
		
		// Add the menu to the template
		$this->template->menu = new View('menu');
	}
	
	public function test()
	{
		$category = new Category_Model(1);
		$language = $category->category_language; 
		
		//echo 'Title:'. $language->title;
		//echo '<br>';		
 
		/*$language = new Category_language_Model(1);
		echo 'Title:'.$language->category->name; 
		echo '<br>';
 
		$category = new Category_Model(1);
		$language = $category->find_related_category_language();
		echo 'Title:'. $language->title;
 
		if($category->has_category_language())
		{
			echo 'Student has car';
		}*/
	}
	
	
	public function index()
	{		
		self::lists(0);
	}
	
	public function lists($parent_id)
	{		
		$model = new Category_Model();
		$category = $model->find_all_by_parent_id($parent_id);
		
		$parent = $model->find($parent_id);
		
		$table = new View('category/lists');
		$table->node_id = $parent_id;
		$table->node_parent_id = $parent->parent_id;
		$table->node_parent_name = $parent->name;
		$table->caption = 'Category List';
		$table->datas = $category;
		
		$this->template->content = $table->render();
	}
	
	public function create($parent_id)
	{
		$form = new Forge(NULL, 'New Category');
		$form->input('name')->label(TRUE);
		$form->input('title_cn')->label('Chinese Title')->rules('required');
		$form->input('title_en')->label('English Title')->rules('required');
		$form->submit('Save');
 
		if($form->validate())
		{
			//Load model and set values
			$category = new Category_Model();
			$category->name = trim($form->name->value) == '' ? $form->title_cn->value : $form->name->value;
			$category->title_cn = $form->title_cn->value;
			$category->title_en = $form->title_en->value;
			$category->parent_id = $parent_id;
			//$category->add_category_language($language);
 
			//Save form into table
			if($category->save())
			{
				url::redirect('category/lists/'.$parent_id);
			}
			else
			{
				echo 'no saving today';
			}
		}
		else
		{
		    $this->template->content = $form->render();
		}
	}
	
	public function edit($id)
	{
		$category = new Category_Model($id);

		$form = new Forge(NULL, 'Edit Category');
		$form->input('name')->label(TRUE)->value($category->name);
		$form->input('title_cn')->label('Chinese Title')->rules('required')->value($category->title_cn);
		$form->input('title_en')->label('English Title')->rules('required')->value($category->title_en);
		$form->submit('Save');
 
		if($form->validate())
		{
			$category->name = $form->name->value;
			$category->title_cn = $form->title_cn->value;
			$category->title_en = $form->title_en->value;
 
			//Save form into table
			if($category->save())
			{
				url::redirect('category/lists/'.$category->parent_id);
			}
			else
			{
				echo 'no saving today';
			}
		}
		else
		{
		    $this->template->content = $form->render();
		}
	}
	
	public function delete($id)
	{
		$category = new Category_Model($id);
		$parent_id = $category->parent_id;
		$category->delete();
		
		url::redirect('category/lists/'.$parent_id);
	}
}